package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.consts.FileManagementContants;
import com.ccp.dev.system.model.FileManagement;
import com.ccp.dev.system.model.FileManagementPermission;
import com.ccp.dev.system.service.FileManagementPermissionService;
import com.ccp.dev.system.service.FileManagementService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文件管理权限 信息操作处理控制器
 * 
 * @author y
 * @date 2020-03-23 10:52:03
 */
@Controller
/**
 * 请将此名称添加到日志开关
 */
@Action(customModel = "文件管理权限")
@RequestMapping("/system/fileManagementPermission")
public class FileManagementPermissionController extends SysBaseController{
	@Resource
	private FileManagementPermissionService fileManagementPermissionService;
	@Resource
	private FileManagementService fileManagementService;

    /**
     * 功能描述: 跳转到按钮权限页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-03-23 10:52:03
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX +"/btnPermission")
    @Action(description="跳转到按钮权限页面", exectype = "页面跳转日志")
    public String btnPermission(HttpServletRequest request,Model model) {
        List<FileManagementPermission> list = fileManagementPermissionService.getPermissionByNodeId(FileManagementContants.PERMISSION_BUTTON, FileManagementContants.BUTTON_NODEID);
        String manageField = "";
        if (list.size() > 0) {
            manageField = list.get(0).getManageField();
        }
        model.addAttribute("manageField", manageField);
        return "/fileManagementPermission/btnPermission";
    }

    /**
     * 功能描述: 目录授权页面
     *
     * @return: java.lang.String
     * @auther: y
     * @date: 2020-03-23 10:52:03
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX + "/nodePermission")
    @Action(description="跳转到目录授权页面", exectype = "页面跳转日志")
    public String nodePermission(HttpServletRequest request, Model model) {
        String nodeId = RequestUtil.getString(request, "nodeId");
        int permissionType = RequestUtil.getInt(request, "permissionType");
        FileManagement fileManagement = fileManagementService.getOneById(nodeId);
        String manageField = "";
        int parentType = fileManagement.getParentType();
        String parentId = fileManagement.getParentId();
        String parentName = fileManagement.getFileName();
        //先查询本身节点的权限，如果没有则查询父节点的
        List<FileManagementPermission> list = fileManagementPermissionService.getPermissionByNodeId(permissionType, nodeId);
        String parManageField = fileManagementPermissionService.getParentPermissionByNodeId(parentId, parentType);
        String parType = "";
        if (StringUtil.isNotEmpty(parManageField)) {
            parType = fileManagementPermissionService.getManageField(parManageField, "type");
        }
        if (list.size() > 0) {
            manageField = list.get(0).getManageField();
        } else {
            manageField = parManageField;
        }
        model.addAttribute("nodeId", nodeId)
                .addAttribute("parentId", fileManagement.getParentId())
                .addAttribute("parentType", parentType)
                .addAttribute("parentName", parentName)
                .addAttribute("manageField", manageField)
                .addAttribute("parType", parType);
        return "/fileManagementPermission/nodePermission";
    }

    /**
    * 功能描述:  数据保存通用方法
    *
    * @param: fileManagementPermission 保存的对象
    * @return: java.lang.Object
    * @auther: y
    * @date: 2020-03-23 10:52:03
    */
    @RequestMapping("/permissionSave")
    @ResponseBody
    @Action(description="保存文件管理权限数据",detail="<#if isAdd>新增<#else>编辑</#if>文件管理权限数据<#if success>成功<#else>失败</#if>")
    public Object permissionSave(HttpServletRequest request) {
        boolean isAdd = false;
        int result = 0;
        int permissionType = RequestUtil.getInt(request, "permissionType",0);
        String manageField = RequestUtil.getString(request, "manageField");
        manageField = StringEscapeUtils.unescapeHtml4(manageField);
        String nodeId = RequestUtil.getString(request, "nodeId");
        List<FileManagementPermission> list = fileManagementPermissionService.getPermissionByNodeId(permissionType, nodeId);
        if (list.size() > 0) {
            FileManagementPermission permission = list.get(0);
            result = fileManagementPermissionService.updatePermission(manageField, permissionType, permission);
        } else {
            isAdd = true;
            if(StringUtils.isNotEmpty(nodeId)&&permissionType!=0){
                 result = fileManagementPermissionService.addPermission(nodeId, permissionType, manageField);
             }
        }
        if(result > 0){
            SysAuditThreadLocalHolder.putParamerter("success",true);
        }else{
            SysAuditThreadLocalHolder.putParamerter("success",false);
        }
        SysAuditThreadLocalHolder.putParamerter("isAdd",isAdd);
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }
}
